package csnd.xinghe.neo4j.server.controller;

import csnd.xinghe.neo4j.server.common.Constants;
import csnd.xinghe.neo4j.server.dto.*;
import csnd.xinghe.neo4j.server.entity.*;
import csnd.xinghe.neo4j.server.service.GraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/neo4j")
public class neo4jController {
    @Autowired
    private GraphService graphService;

    // 查询员工或企业的关系（最多一层）
    @RequestMapping("/relationships")
    public Map<String, Object> getEmployeeRelationships(@RequestBody GetRelationshipRequest request) {
        Map<String, Object> response = new HashMap<>();
        GraphEntitiy result;
        try {
            if (request.getType() == 1) {
                result = graphService.getEmployeeWithRelationships(request.getName());
            } else {
                result = graphService.getCompanyWithRelationships(request.getName());
            }
            response.put("message", Constants.SUCCESS_MESSAGE);
            response.put("code", Constants.SUCCESS_CODE);
            response.put("result", result);
        } catch (Exception e) {
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("message", Constants.ERROR_MESSAGE);
            errorResult.put("code", Constants.ERROR_CODE);
            return errorResult;
        }
        return response;
    }


    //获取两个节点之间的关系
    @RequestMapping("/getNodeRelation")
    public Map<String, Object> getNodeRelation(@RequestBody GetNodeRelationRequest request) {
        Map<String, Object> response = new HashMap<>();
        List<EdgeEntity> result;
        try {
            result = graphService.getNodeRelation(request.getFirstNodeId(), request.getSecondNodeId());
            response.put("message", Constants.SUCCESS_MESSAGE);
            response.put("code", Constants.SUCCESS_CODE);
            response.put("result", result);
        } catch (Exception e) {
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("message", Constants.ERROR_MESSAGE);
            errorResult.put("code", Constants.ERROR_CODE);
            return errorResult;
        }
        return response;
    }


    // 创建图
    @PostMapping("/createGraph")
    public Map<String, Object> createGraph(@RequestBody CreateGraphRequest request) {
        Map<String, Object> result;
        try {
            result = graphService.createGraph(request);

        } catch (Exception e) {
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("message", Constants.ERROR_MESSAGE);
            errorResult.put("code", Constants.ERROR_CODE);
            return errorResult;
        }
        return result;
    }

    /**
     * 首页搜索
     * @param name
     * @return
     */
    @PostMapping("/homePageSearch")
    public Map<String, Object> homePageSearch(@RequestBody String name) {
        Map<String, Object> response = new HashMap<>();
        Map<String, Object> result;
        try {
            List<HomePageSearchNodeEntity> homePageSearchNodeEntities = graphService.homePageSearch(name);
            response.put("message", Constants.SUCCESS_MESSAGE);
            response.put("code", Constants.SUCCESS_CODE);
            response.put("result", homePageSearchNodeEntities);
        } catch (Exception e) {
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("message", Constants.ERROR_MESSAGE);
            errorResult.put("status", Constants.ERROR_CODE);
            return errorResult;
        }
        return response;
    }


    /**
     * 知识图谱目录
     * @param request
     * @return
     */
    @PostMapping("/directory")
    public Map<String, Object> directory(@RequestBody DirectoryRequest request) {
        Map<String, Object> response = new HashMap<>();
        DirectoryResultEntity directory = graphService.directory(request);
        response.put("message", Constants.SUCCESS_MESSAGE);
        response.put("code", Constants.SUCCESS_CODE);
        response.put("data", directory);
        return response;
    }

    /**
     * 知识图谱
     * @param request
     * @return
     */
    @PostMapping("/graph")
    public Map<String, Object> graph(@RequestBody GraphRequest request) {
        Map<String, Object> response = new HashMap<>();
        GraphResultEntity graph = graphService.graph(request);
        response.put("message", Constants.SUCCESS_MESSAGE);
        response.put("code", Constants.SUCCESS_CODE);
        response.put("data", graph);
        return response;
    }



    /**
     * 基于nodeId和关系标识的全量替换图数据接口
     */
    @PostMapping("/updateGraph")
    public Map<String, Object> updateGraph(@RequestBody CreateGraphRequest request) {
        Map<String, Object> result = graphService.updateGraph(request);
        return result;
    }


    //创建节点
    @PostMapping("/createNodes")
    public ResponseEntity<Map<String, Object>> createNodes(@RequestBody CreateGraphRequest request) {
        try {
            Map<String, Object> result = graphService.createNode(request);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("success", false);
            errorResult.put("message", "图数据创建失败: " + e.getMessage());
            return ResponseEntity.status(500).body(errorResult);
        }
    }

    //创建关系
    @PostMapping("/createRelationships")
    public ResponseEntity<Map<String, Object>> createRelationships(@RequestBody CreateGraphRequest request) {
        try {
            Map<String, Object> result = graphService.createRelationships(request);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("success", false);
            errorResult.put("message", "图数据创建失败: " + e.getMessage());
            return ResponseEntity.status(500).body(errorResult);
        }
    }


}

